package com.tbit.uqbike.webmanager.controller.interceptor;

import com.tbit.uqbike.enums.SystemCodeEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.service.base.SuperPswService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * created by Kang on 2019/3/28 13:55
 * description:拦截器配置
 */
public class SuperPswInterceptor extends HandlerInterceptorAdapter {
    @Autowired
    private SuperPswService superPswService;

    /**
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        String superPsw = request.getParameter("superPsw");
        String path = request.getRequestURI();
        LoginInfo li = (LoginInfo) request.getAttribute(HttpHeadName.REQUEST_ATTR_NAME_USER);
        if (null == superPsw) {
            throw new BaseException(SystemCodeEnum.NEED_SUPER_CODE);
        }
        if (superPswService.superPswCheck(path, li, superPsw)) {
            return true;
        } else {
            throw new BaseException(SystemCodeEnum.SUPER_CODE_ERROR);
        }
    }
}
